<?php

namespace Admin\Model;
use Admin\Model\BaseModel;
class AdminAtomModel extends BaseModel
{
    public $tableName = 'admin_atom';
    public $_validate = array(
        array('name', '1,45', '权限名必须在1~45之间', 0, 'length', 3),
        array('func_name', '0,45', '方法名必须在0~45之间', 0, 'length', 3),
        array('func_data', '0,45', '方法数据必须在0~45之间', 0, 'length', 3)
    );

    /**
     * 得到权限详情
     * @param int $id
     * @return Array
     * @throws Exception
     */
    public function getPermissionAtomInfo($id)
    {
        $atomInfo = $this->where(array('id' => $id))->find();
        if (false === $atomInfo) {
            return array();
        }
        return $atomInfo;
    }

    /**
     * 通过方法名找详情列表
     * @param string 　$funcName
     * @return Array
     * @throws Exception
     */
    public function getAtomInfo4Url($actionName, $funcData)
    {
        $return = false;
        $where['func_name'] = $actionName;
        $atomArray = $this->where($where)->select();
        foreach ($atomArray as $v) {
            if ($actionName == $v['func_name'] &&
                (empty($v['func_data']) || stripos($funcData, $v['func_data']) !== FALSE)) {
                $return = $v;
            }
        }
        return $return;
    }

    /**
     * 通过class_id得到所有相关权限
     * @param sting $menu_main_class 要取class_id子目录权限的，传class_id%
     * @param type $orderby
     * @return type
     * @throws Exception
     */
    public function getAtomList4MenuID($menu_main_class = '', $orderby = 'orderby ASC', $where = array())
    {
        if (!empty($menu_main_class) && strpos($menu_main_class, '%') === FALSE) {
            $where['menu_main_class'] = $menu_main_class;
        }
        if (!empty($menu_main_class) && strpos($menu_main_class, '%') !== FALSE) {
            $where['menu_main_class'] = array('like', $menu_main_class);
        }
        $atomList = $this->where($where)->order($orderby)->select();
        if (false === $atomList) {
            throw new \Exception('没有相关权限数据');
        }
        return $atomList;
    }

    /**
     * 通过权限id得到目录class_id数组
     * @param type $id
     * @return int
     */
    public function getClassArray4id($id, $check_class_id = '')
    {
        $classArray = array();
        $permissionAtomInfo = $this->getPermissionAtomInfo($id);
        if (!empty($permissionAtomInfo['menu_main_class'])) {
            $class_id = $permissionAtomInfo['menu_main_class'];
            $len = strlen($class_id);
            for ($i = 2; $i <= $len; $i += 2) {
                $faClass = substr($class_id, 0, $i);
                if (substr($faClass, 0, strlen($check_class_id)) == $check_class_id) {
                    $classArray[$faClass] = 1;
                }
            }
        }
        return $classArray;
    }

    /**
     * 存储操作
     * @param type $dataArray
     * @return type
     * @throws Exception
     */
    public function doSave($dataArray)
    {
        if (empty($dataArray['id'])) {
            $dataArray['create_time'] = date("Y-m-d H:i:s");
            $dataArray['orderby'] = $this->getAtomOrder($dataArray['menu_main_class']) + 1;
        }
        $dataArray['update_time'] = date("Y-m-d H:i:s");

        if (false === $this->create($dataArray)) {
            throw new \Exception($this->getError());
        }
        if (empty($dataArray['id'])) {
            $res = $this->add($dataArray);
        } else {
            $res = $this->save($dataArray);
        }
        return $res;
    }

    /**
     * 删除信息
     * @param type $id
     * @return type
     */
    public function delAtom($id)
    {
        //故意查询一下，如果没有info缓存,_after_del方法会执行不正常
        $atomInfo = $this->getPermissionAtomInfo($id);
        if (empty($atomInfo)) {
            return TRUE;
        }
        $where['id'] = $id;
        $res = $this->where($where)->delete();
        return $res;
    }

    /**
     * 取得某类别当前排序
     * @param string $menu_main_class
     * @return int
     */
    public function getAtomOrder($menu_main_class)
    {
        $orderby = $this->where('menu_main_class = ' . $menu_main_class)->max('orderby');
        $orderby = empty($orderby) ? 0 : $orderby;
        return $orderby;
    }

    /**
     * 将一串“，”号隔开的字符，分解成对应的权限详情
     * @param String $atomStr
     * @return array
     */
    public function getAtom4Str($atomStr)
    {
        $atomList = array();
        $atomStr = str_replace('，', ',', $atomStr);
        $atomArray = explode(',', $atomStr);
        foreach ($atomArray as $key => $value) {
            $atomList[$value] = $this->getPermissionAtomInfo($value);
        }
        return $atomList;
    }

    /**
     * 将一串“，”号隔开的字符，得到权限名字
     * @param String $atomStr
     * @return array
     */
    public function getAtomName4Str($atomStr)
    {
        $atomList = $this->getAtom4Str($atomStr);
        $atomName = '';
        foreach ($atomList as $key => $value) {
            $atomName .= $value['name'] . ',';
        }
        return substr($atomName, 0, -1);
    }

    protected function _after_update($data, $options)
    {

    }

    protected function _after_insert($data, $options)
    {

    }

    protected function _after_delete($data, $options)
    {

    }
}

?>